import org.junit.Test;

/**
 * @author jcl_hqu
 * @date 2021/3/19 16:07
 */
public class ShareData {
    private int data = 0;
    private boolean flag_c=false;
    private boolean flag_p=false;
    public  void produce(String ThreadName) throws InterruptedException {
        int count = 0;
        while (count < 20) {
            while (flag_c) ;
            flag_p = true;
            System.out.println("生产者工作,此时占用共享内存情况：生产者"+flag_p+"  消费者"+flag_c);
            if (data == 100) {
                System.out.println("(" + ThreadName + ")" + "生产者：不生产 ; 库存" + data);
            } else {
                count++;
                data += 1;
                System.out.println("(" + ThreadName + ")" + "生产者：生产后 ; 库存" + data + " 次数" + count);
            }
            flag_p = false;
        }
    }

    public  void consume(String ThreadName) throws InterruptedException {
        int count = 0;
        while (count < 20) {
            while (flag_p);
            flag_c=true;
            System.out.println("消费者工作,此时占用共享内存情况：生产者"+flag_p+"  消费者"+flag_c);
            if (data == 0) {
                    System.out.println("(" + ThreadName + ")" + "消费者：不消费; 库存" + data);
            } else {
                data -= 1;
                count++;
                System.out.println("(" + ThreadName + ")" + "消费者：消费后; 库存" + data + " 次数" + count);
            }
            flag_c=false;
        }
    }
}
